package com.zx.oa.listener;

import jakarta.servlet.annotation.WebListener;
import jakarta.servlet.http.HttpSessionAttributeListener;
import jakarta.servlet.http.HttpSessionBindingEvent;
import jakarta.servlet.http.HttpSessionEvent;
import jakarta.servlet.http.HttpSessionListener;

@WebListener
public class OnlineCounterListener  implements HttpSessionListener,
        HttpSessionAttributeListener {
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        System.out.println("========session 创建===========");
        //在线人数+1
//        OnlineCounter.raise();
    }
    @Override
    public void attributeAdded(HttpSessionBindingEvent event) {
        System.out.println("session名称:====="+event.getName());
        if (null!=event.getName()&&!"".equals(event.getName())) {
		if ("loginName".equals(event.getName())) {
			System.out.println("人数+1");
			//在线人数+1
			OnlineCounter.raise();
		}
        }
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent event) {
        if (null!=event.getName()&&!"".equals(event.getName())) {
            if ("loginName".equals(event.getName())) {
                System.out.println("人数-1");
                //在线人数-1
                OnlineCounter.reduce();
            }
        }
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        //在线人数-1
        OnlineCounter.reduce();
    }
}
